{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using JuliaRunClient\n",
    "ctx = Context()\n",
    "nb = self()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "initParallel()\n",
    "\n",
    "NWRKRS = 2\n",
    "println(\"scale up to $NWRKRS\")\n",
    "\n",
    "@result setJobScale(ctx, nb, NWRKRS)\n",
    "waitForWorkers(NWRKRS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using StatsBase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@everywhere using StatsBase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@everywhere function montecarlo(howmany, data_generator, bins)\n",
    "    h  = Histogram(bins)\n",
    "    for i=1:howmany\n",
    "        push!(h, data_generator() )\n",
    "    end\n",
    "    return h.weights\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = @parallel (+) for i=1:nworkers()\n",
    "  montecarlo(100000, randn, -3:.1:3)\n",
    "end;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@everywhere  function tracywidom_sample(β=2,n=200)\n",
    "  h=n^(-1/3)\n",
    "  x=[0:h:10;]\n",
    "  N=length(x)\n",
    "  d=(-2/h^2 .-x) +  2/sqrt(h*β)*randn(N) # diagonal\n",
    "  e=ones(N-1)/h^2                   # subdiagonal\n",
    "  eigvals(SymTridiagonal(d,e))[N]\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plot()\n",
    "for β = [1,2,4]\n",
    " bins = -4:.05:0.95\n",
    " w=\n",
    "  @parallel (+) for i=1:nworkers()\n",
    "      montecarlo(10000,()->tracywidom_sample(β), -4:.05:1)\n",
    "  end;\n",
    "plot!(bins, w/sum(w)*bins.step.hi)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot!()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Scale down\n",
    "@result setJobScale(ctx, self(), 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nworkers()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.0",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
